package com.xnx3.banmahuishou.agency.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xnx3.BaseVO;
import com.xnx3.DateUtil;
import com.xnx3.DoubleUtil;
import com.xnx3.banmahuishou.core.entity.Qishou;
import com.xnx3.banmahuishou.core.entity.QishouLeaveRecord;
import com.xnx3.banmahuishou.core.util.AppUtil;
import com.xnx3.banmahuishou.core.vo.HumanEffectAnalyzeListVO;
import com.xnx3.banmahuishou.core.vo.HumanEffectAnalyzeVO;
import com.xnx3.banmahuishou.core.vo.OrderAnalyzeListVO;
import com.xnx3.banmahuishou.core.vo.OrderAnalyzeVO;
import com.xnx3.banmahuishou.core.vo.RiderAnalyzeListVO;
import com.xnx3.banmahuishou.core.vo.RiderAnalyzeVO;
import com.xnx3.banmahuishou.core.vo.WorkAnalyzeListVO;
import com.xnx3.banmahuishou.core.vo.WorkAnalyzePicVO;
import com.xnx3.banmahuishou.core.vo.WorkAnalyzeVO;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.util.ActionLogUtil;
import com.xnx3.j2ee.util.Page;
import com.xnx3.j2ee.util.Sql;
import com.xnx3.wangmarket.shop.core.entity.Order;

import net.sf.json.JSONObject;

/**
 * 数据统计
 * @author 王晓龙
 */
@Controller(value="BanmahuishouAgencyStatisticsController")
@RequestMapping("/agency/statistics/")
public class StatisticsController extends BaseController {
	@Resource
	private SqlService sqlService;



	/**
	 * 代理商后台---订单数据分析
	 * @param startTime		筛选日期开始时间
	 * @param endTime		筛选日期结束时间
	 * @author zxy
	 * @throws ParseException 
	 */
	@ResponseBody
	@RequestMapping(value = "getOrderAnalyze.json",method = {RequestMethod.POST})
	public OrderAnalyzeListVO getOrderAnalyze(HttpServletRequest request,Model model,
			@RequestParam(value = "startTime",required = true,defaultValue = "0") Integer startTime,
			@RequestParam(value = "endTime",required = true,defaultValue = "0") Integer endTime) throws ParseException {
		int id = getUserId();

		OrderAnalyzeListVO vo = new OrderAnalyzeListVO();
		if (startTime > 0 && endTime <= 0) {
			vo.setBaseVO(BaseVO.FAILURE, "请选择时间范围");
			return vo;
		}
		if (startTime <= 0 && endTime > 0) {
			vo.setBaseVO(BaseVO.FAILURE, "请选择时间范围");
			return vo;
		}
		if (endTime < startTime) {
			vo.setBaseVO(BaseVO.FAILURE, "请选择正确的时间范围");
			return vo;
		}

		// 日期天数, 如果没有选择时间筛选条件 默认七天
		int i = 7;
		if (startTime > 0 && endTime > 0) {
			String startday = DateUtil.intToString(startTime, "yyyyMMdd");
			String endday = DateUtil.intToString(endTime, "yyyyMMdd");
			i = AppUtil.daysBetween(startday, endday, "yyyyMMdd");
			// 最长30天
			if (i > 30) {
				vo.setBaseVO(BaseVO.FAILURE, "时间跨度最大不能超过30天");
				return vo;
			}
			// 最短1天
			if (i == 0) {
				vo.setBaseVO(BaseVO.FAILURE, "时间跨度最小1天");
				return vo;
			}
			// 获取天数
			i = i + 1;
			endTime = endTime + 86400;
		}else {
			endTime = DateUtil.getDateZeroTime(DateUtil.timeForUnix10());// 当日零点的时间戳 
			startTime = endTime - 7 * 86400;// 一周之前时间戳
		}
		// 日期数组
		int[] dateI = new int[i];
		Integer firstday = Integer.valueOf(DateUtil.intToString(startTime, "yyyyMMdd"));
		dateI[0] = firstday;
		for (int j = 1; j < dateI.length; j++) {
			// 在上个元素天数的基础上加一天
			dateI[j] = dateI[j-1] + 1;
			// 当前日期
			String dateString = dateI[j] + "";
			// 判断当前日是否是当前月最后一日
			int monthLastDay = AppUtil.getMonthLastDay(DateUtil.StringToInt(dateI[j-1] + "", "yyyyMMdd"), "yyyyMMdd");
			if (dateI[j] - monthLastDay == 1) {
				// 判断当前月是否是当前年最后一月
				if (Objects.equals(dateString.substring(4, 6), "12")) {
					// 跨年跨月
					Integer substring = Integer.valueOf(dateString.substring(0, 4));
					String yearFirst = AppUtil.getYearFirst(substring + 1,"yyyyMMdd");
					dateI[j] = Integer.valueOf(yearFirst);
				}else {
					// 只跨月不跨年
					String firstDayOfNextMonth = AppUtil.getFirstDayOfNextMonth(dateI[j-1] + "", "yyyyMMdd");
					dateI[j] = Integer.valueOf(firstDayOfNextMonth);
				}
			}
		}
		vo.setDateI(dateI);
		// 获取在这个时间段的订单
		String ordernow = "SELECT * FROM shop_order WHERE addtime > " + startTime + " AND  addtime < " + endTime + " AND dailishang_userid = " + id;
		List<Order> list = sqlService.findBySqlQuery(ordernow, Order.class);

		// 日期-数据对象
		Map<Integer, OrderAnalyzeVO> map = new TreeMap<Integer, OrderAnalyzeVO>();

		for (Order order : list) {
			// 根据日期进行数据分发
			Integer orderday = Integer.valueOf(DateUtil.intToString(order.getAddtime(), "yyyyMMdd"));
			// 某天的数据,用于列表
			OrderAnalyzeVO orderAnalyzeVO = map.get(orderday);
			if (orderAnalyzeVO == null) {
				orderAnalyzeVO = new OrderAnalyzeVO();
				orderAnalyzeVO.setDingdanShuliang(1);
				orderAnalyzeVO.setJunjia(order.getPayMoney());
				orderAnalyzeVO.setJunzhong(order.getAllWeight());
				if (ObjectUtils.equals(order.getIsNew(), 1)) {
					// 新用户下单数
					orderAnalyzeVO.setNewUserNum(1);
					// 老用户下单数
					orderAnalyzeVO.setOldUserNum(0);
				}else {
					// 新用户下单数
					orderAnalyzeVO.setNewUserNum(0);
					// 老用户下单数
					orderAnalyzeVO.setOldUserNum(1);
				}
				if (ObjectUtils.equals(order.getState(), Order.STATE_FINISH) || ObjectUtils.equals(order.getState(), Order.STATE_EVALUATED)) {
					// 完成量
					orderAnalyzeVO.setWanchengNum(1);
					// 取消量
					orderAnalyzeVO.setQuxiaoNum(0);
					// 异常量
					orderAnalyzeVO.setYichangNum(0);
					// 待取量
					orderAnalyzeVO.setDaiquNum(0);
				}else if (ObjectUtils.equals(order.getState(), Order.STATE_CANCEL)) {
					// 完成量
					orderAnalyzeVO.setWanchengNum(0);
					// 取消量
					orderAnalyzeVO.setQuxiaoNum(1);
					// 异常量
					orderAnalyzeVO.setYichangNum(0);
					// 待取量
					orderAnalyzeVO.setDaiquNum(0);
				}else if (ObjectUtils.equals(order.getState(), Order.STATE_DAIQUHUO) && order.getReserveEndTime() > DateUtil.timeForUnix10()) {
					// 完成量
					orderAnalyzeVO.setWanchengNum(0);
					// 取消量
					orderAnalyzeVO.setQuxiaoNum(0);
					// 异常量
					orderAnalyzeVO.setYichangNum(0);
					// 待取量
					orderAnalyzeVO.setDaiquNum(1);
				}else {
					// 完成量
					orderAnalyzeVO.setWanchengNum(0);
					// 取消量
					orderAnalyzeVO.setQuxiaoNum(0);
					// 异常量
					orderAnalyzeVO.setYichangNum(1);
					// 待取量
					orderAnalyzeVO.setDaiquNum(0);
				}
				// 日期
				orderAnalyzeVO.setRiqi(Integer.valueOf(DateUtil.intToString(order.getAddtime(), "yyyyMMdd")));
				// 添加到map
				map.put(orderday, orderAnalyzeVO);

			}else {
				orderAnalyzeVO.setDingdanShuliang(orderAnalyzeVO.getDingdanShuliang() + 1);
				orderAnalyzeVO.setJunjia(orderAnalyzeVO.getJunjia() + order.getPayMoney());
				orderAnalyzeVO.setJunzhong(orderAnalyzeVO.getJunzhong() + order.getAllWeight());
				if (ObjectUtils.equals(order.getIsNew(), 1)) {
					// 新用户下单数
					orderAnalyzeVO.setNewUserNum(orderAnalyzeVO.getNewUserNum() + 1);
				}else {
					// 老用户下单数
					orderAnalyzeVO.setOldUserNum(orderAnalyzeVO.getOldUserNum() + 1);
				}
				if (ObjectUtils.equals(order.getState(), Order.STATE_FINISH) || ObjectUtils.equals(order.getState(), Order.STATE_EVALUATED)) {
					// 完成量
					orderAnalyzeVO.setWanchengNum(orderAnalyzeVO.getWanchengNum() + 1);
				}else if (ObjectUtils.equals(order.getState(), Order.STATE_CANCEL)) {
					// 取消量
					orderAnalyzeVO.setQuxiaoNum(orderAnalyzeVO.getQuxiaoNum() + 1);
				}else if (ObjectUtils.equals(order.getState(), Order.STATE_DAIQUHUO) && order.getReserveEndTime() > DateUtil.timeForUnix10()) {
					// 待取量
					orderAnalyzeVO.setDaiquNum(orderAnalyzeVO.getDaiquNum() + 1);
				}else {
					// 异常量
					orderAnalyzeVO.setYichangNum(orderAnalyzeVO.getYichangNum() + 1);
				}
				// 添加到map
				map.put(orderday, orderAnalyzeVO);
			}
		}

		// 根据日期补全数据
		for (int j = 0; j < dateI.length; j++) {
			OrderAnalyzeVO analyzeVO = map.get(dateI[j]);
			if (analyzeVO == null) {
				analyzeVO = new OrderAnalyzeVO(dateI[j], 0, 0, 0, 0, 0, 0, 0, 0, 0);
				map.put(dateI[j], analyzeVO);
			}
		}

		// 数据列表--返回列表使用
		List<OrderAnalyzeVO> analyzeVOs = new ArrayList<OrderAnalyzeVO>();
		map.forEach((k,v) ->{
			analyzeVOs.add(v);
		});

		vo.setList(analyzeVOs);

		// 订单量数组,长度为日期天数
		int[] dingdanlaingArr = new int[i];
		int[] newUserArr = new int[i];
		int[] oldUserArr = new int[i];
		int[] wanchengArr = new int[i];
		int[] quxiaoArr = new int[i];
		int[] daiquArr = new int[i];
		int[] yichangArr = new int[i];
		double[] junjiaArr = new double[i];
		double[] junzhongArr = new double[i];
		if (analyzeVOs.size() - i == 0) {
			for (int j = 0; j < analyzeVOs.size(); j++) {
				dingdanlaingArr[j] = analyzeVOs.get(j).getDingdanShuliang();
				newUserArr[j] = analyzeVOs.get(j).getNewUserNum();
				oldUserArr[j] = analyzeVOs.get(j).getOldUserNum();
				wanchengArr[j] = analyzeVOs.get(j).getWanchengNum();
				quxiaoArr[j] = analyzeVOs.get(j).getQuxiaoNum();
				daiquArr[j] = analyzeVOs.get(j).getDaiquNum();
				yichangArr[j] = analyzeVOs.get(j).getYichangNum();
				if (analyzeVOs.get(j).getWanchengNum() > 0 && analyzeVOs.get(j).getJunjia() > 0) {
					junjiaArr[j] = DoubleUtil.doubleSplit(analyzeVOs.get(j).getJunjia() / analyzeVOs.get(j).getWanchengNum() / 100.0,2);
				}else {
					junjiaArr[j] = DoubleUtil.doubleSplit(analyzeVOs.get(j).getJunjia() / 100.0,2);
				}
				if (analyzeVOs.get(j).getDingdanShuliang() > 0 && analyzeVOs.get(j).getJunzhong() > 0) {
					junzhongArr[j] = DoubleUtil.doubleSplit(analyzeVOs.get(j).getJunzhong() / analyzeVOs.get(j).getDingdanShuliang() / 1000.0,2);
				}else {
					junzhongArr[j] = DoubleUtil.doubleSplit(analyzeVOs.get(j).getJunzhong() / 1000.0,2);
				}
			}
		}else {
			// 数据错误
			vo.setBaseVO(BaseVO.FAILURE, "数据异常,请联系技术人员");
			return vo;
		}

		// 折线图数据
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("订单总数", dingdanlaingArr);
		jsonObject.put("新用户下单数", newUserArr);
		jsonObject.put("老用户下单数", oldUserArr);
		jsonObject.put("完成量", wanchengArr);
		jsonObject.put("取消量", quxiaoArr);
		jsonObject.put("待取量", daiquArr);
		jsonObject.put("异常量", yichangArr);
		jsonObject.put("订单均价", junjiaArr);
		jsonObject.put("订单均量", junzhongArr);

		vo.setJsonObject(jsonObject);
		//日志记录
		ActionLogUtil.insert(request, id, "查看代理商后台---订单数据分析");
		return vo;
	}
	/**
	 * 代理商后台---用户数据下单分析
	 * @param startTime		筛选日期开始时间
	 * @param endTime		筛选日期结束时间
	 * @author zxy
	 * @throws ParseException 
	 */
	@ResponseBody
	@RequestMapping(value = "getUserAnalyze.json",method = {RequestMethod.POST})
	public OrderAnalyzeListVO getUserAnalyze(HttpServletRequest request,Model model,
			@RequestParam(value = "startTime",required = true,defaultValue = "0") Integer startTime,
			@RequestParam(value = "endTime",required = true,defaultValue = "0") Integer endTime) throws ParseException {
		int id = getUserId() ;//获取代理商id
		OrderAnalyzeListVO vo = new OrderAnalyzeListVO();
		if (startTime > 0 && endTime <= 0) {
			vo.setBaseVO(BaseVO.FAILURE, "请选择时间范围");
			return vo;
		}
		if (startTime <= 0 && endTime > 0) {
			vo.setBaseVO(BaseVO.FAILURE, "请选择时间范围");
			return vo;
		}
		if (endTime < startTime) {
			vo.setBaseVO(BaseVO.FAILURE, "请选择正确的时间范围");
			return vo;
		}

		// 日期天数, 如果没有选择时间筛选条件 默认七天
		int i = 7;
		if (startTime > 0 && endTime > 0) {
			String startday = DateUtil.intToString(startTime, "yyyyMMdd");
			String endday = DateUtil.intToString(endTime, "yyyyMMdd");
			i = AppUtil.daysBetween(startday, endday, "yyyyMMdd");
			// 最长30天
			if (i > 30) {
				vo.setBaseVO(BaseVO.FAILURE, "时间跨度最大不能超过30天");
				return vo;
			}
			// 最短1天
			if (i == 0) {
				vo.setBaseVO(BaseVO.FAILURE, "时间跨度最小1天");
				return vo;
			}
			// 获取天数
			i = i + 1;
			endTime = endTime + 86400;
		}else {
			endTime = DateUtil.getDateZeroTime(DateUtil.timeForUnix10());// 当日零点的时间戳 
			startTime = endTime - 7 * 86400;// 一周之前时间戳
		}
		// 日期数组
		int[] dateI = new int[i];
		Integer firstday = Integer.valueOf(DateUtil.intToString(startTime, "yyyyMMdd"));
		dateI[0] = firstday;
		for (int j = 1; j < dateI.length; j++) {
			// 在上个元素天数的基础上加一天
			dateI[j] = dateI[j-1] + 1;
			// 当前日期
			String dateString = dateI[j] + "";
			// 判断当前日是否是当前月最后一日
			int monthLastDay = AppUtil.getMonthLastDay(DateUtil.StringToInt(dateI[j-1] + "", "yyyyMMdd"), "yyyyMMdd");
			if (dateI[j] - monthLastDay == 1) {
				// 判断当前月是否是当前年最后一月
				if (Objects.equals(dateString.substring(4, 6), "12")) {
					// 跨年跨月
					Integer substring = Integer.valueOf(dateString.substring(0, 4));
					String yearFirst = AppUtil.getYearFirst(substring + 1,"yyyyMMdd");
					dateI[j] = Integer.valueOf(yearFirst);
				}else {
					// 只跨月不跨年
					String firstDayOfNextMonth = AppUtil.getFirstDayOfNextMonth(dateI[j-1] + "", "yyyyMMdd");
					dateI[j] = Integer.valueOf(firstDayOfNextMonth);
				}
			}
		}
		vo.setDateI(dateI);
		// 获取在这个时间段的订单
		String ordernow = "SELECT * FROM shop_order WHERE addtime > " + startTime + " AND addtime < " + endTime + " AND dailishang_userid = " + id;
		List<Order> list = sqlService.findBySqlQuery(ordernow, Order.class);

		// 日期-数据对象
		Map<Integer, OrderAnalyzeVO> map = new TreeMap<Integer, OrderAnalyzeVO>();

		for (Order order : list) {
			// 根据日期进行数据分发
			Integer orderday = Integer.valueOf(DateUtil.intToString(order.getAddtime(), "yyyyMMdd"));
			// 某天的数据,用于列表
			OrderAnalyzeVO orderAnalyzeVO = map.get(orderday);
			if (orderAnalyzeVO == null) {
				orderAnalyzeVO = new OrderAnalyzeVO();
				orderAnalyzeVO.setDingdanShuliang(1);
				if (ObjectUtils.equals(order.getIsNew(), 1)) {
					// 新用户下单数
					orderAnalyzeVO.setNewUserNum(1);
					// 老用户下单数
					orderAnalyzeVO.setOldUserNum(0);
				}else {
					// 新用户下单数
					orderAnalyzeVO.setNewUserNum(0);
					// 老用户下单数
					orderAnalyzeVO.setOldUserNum(1);
				}

				// 日期
				orderAnalyzeVO.setRiqi(Integer.valueOf(DateUtil.intToString(order.getAddtime(), "yyyyMMdd")));
				// 添加到map
				map.put(orderday, orderAnalyzeVO);

			}else {
				orderAnalyzeVO.setDingdanShuliang(orderAnalyzeVO.getDingdanShuliang() + 1);
				if (ObjectUtils.equals(order.getIsNew(), 1)) {
					// 新用户下单数
					orderAnalyzeVO.setNewUserNum(orderAnalyzeVO.getNewUserNum() + 1);
				}else {
					// 老用户下单数
					orderAnalyzeVO.setOldUserNum(orderAnalyzeVO.getOldUserNum() + 1);
				}
				// 添加到map
				map.put(orderday, orderAnalyzeVO);
			}
		}

		// 根据日期补全数据
		for (int j = 0; j < dateI.length; j++) {
			OrderAnalyzeVO analyzeVO = map.get(dateI[j]);
			if (analyzeVO == null) {
				analyzeVO = new OrderAnalyzeVO(dateI[j], 0, 0, 0, 0, 0, 0, 0, 0, 0);
				map.put(dateI[j], analyzeVO);
			}
		}

		// 数据列表--返回列表使用
		List<OrderAnalyzeVO> analyzeVOs = new ArrayList<OrderAnalyzeVO>();
		map.forEach((k,v) ->{
			analyzeVOs.add(v);
		});

		vo.setList(analyzeVOs);

		// 订单量数组,长度为日期天数
		int[] dingdanlaingArr = new int[i];
		int[] newUserArr = new int[i];
		int[] oldUserArr = new int[i];

		if (analyzeVOs.size() - i == 0) {
			for (int j = 0; j < analyzeVOs.size(); j++) {
				dingdanlaingArr[j] = analyzeVOs.get(j).getDingdanShuliang();
				newUserArr[j] = analyzeVOs.get(j).getNewUserNum();
				oldUserArr[j] = analyzeVOs.get(j).getOldUserNum();
			}
		}else {
			// 数据错误
			vo.setBaseVO(BaseVO.FAILURE, "数据异常,请联系技术人员");
			return vo;
		}

		// 柱状图数据
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("新用户下单数", newUserArr);
		jsonObject.put("老用户下单数", oldUserArr);

		vo.setJsonObject(jsonObject);
		//日志记录
		ActionLogUtil.insert(request, id , "查看代理商后台---用户数据分析");
		return vo;
	}


	/**
	 * 代理商后台-骑手考勤分析
	 * @param startTime		筛选日期开始时间
	 * @param endTime		筛选日期结束时间
	 * @author zxy
	 */
	@ResponseBody
	@RequestMapping(value = "getRiderAnalyze.json",method = {RequestMethod.POST})
	public RiderAnalyzeListVO getRiderAnalyze(HttpServletRequest request,
			@RequestParam(value = "startTime",required = true,defaultValue = "0") Integer startTime,
			@RequestParam(value = "endTime",required = true,defaultValue = "0") Integer endTime) {
		int id = getUserId() ;//获取代理商id
		Sql sql = new Sql(request);
		RiderAnalyzeListVO vo = new RiderAnalyzeListVO();
		if (startTime > 0 && endTime <= 0) {
			vo.setBaseVO(BaseVO.FAILURE, "请选择时间范围");
			return vo;
		}
		if (startTime <= 0 && endTime > 0) {
			vo.setBaseVO(BaseVO.FAILURE, "请选择时间范围");
			return vo;
		}
		// 日期天数, 如果没有选择时间筛选条件 默认是本月的
		if (startTime > 0 && endTime > 0) {
			int parseInt = (endTime - startTime) / (3600 * 24);
			// 最短1天
			if (parseInt == 0) {
				vo.setBaseVO(BaseVO.FAILURE, "时间跨度最小1天");
				return vo;
			}
			// 最长30天
			if (parseInt > 30) {
				vo.setBaseVO(BaseVO.FAILURE, "时间跨度最大不能超过30天");
				return vo;
			}
		}else {
			endTime = DateUtil.timeForUnix10();// 当前时间戳
			startTime = (int)AppUtil.getMonthBegin4Linux();// 本月一日零点
		}
		//获取该用户可以查看的骑手并分页
		sql.appendWhere(" dailishang_userid = " + id);
		int count = sqlService.count("laji_qishou", sql.getWhere());
		Page page = new Page(count, 15, request);
		sql.setSelectFromAndPage("SELECT * FROM laji_qishou ", page);
		sql.setDefaultOrderBy("id ASC");
		List<Qishou> qishouList = sqlService.findBySql(sql,Qishou.class);
		// 放骑手用户id
		StringBuffer buffer = new StringBuffer("0");

		// 数据容器 k: 骑手id,v: RiderAnalyzeVO对象
		Map<Integer, RiderAnalyzeVO> map = new HashMap<Integer, RiderAnalyzeVO>();
		List<RiderAnalyzeVO> list = new ArrayList<RiderAnalyzeVO>();
		for (Qishou qishou : qishouList) {
			buffer.append("," + qishou.getId());
			RiderAnalyzeVO analyze = new RiderAnalyzeVO(qishou.getName(), 0, 0.0);
			map.put(qishou.getId(), analyze);
			list.add(analyze);
		}

		//查询骑手请假记录表内已通过审核且函数处理完成的请假记录
		String qijiaFinish = "SELECT * FROM laji_qishou_leave_record WHERE state = " 
				+ QishouLeaveRecord.STATE_FINISH + " AND create_time > " + startTime + " AND create_time < " + endTime + " AND qishou_userid IN(" + buffer + ")";
		List<QishouLeaveRecord> finishList = sqlService.findBySqlQuery(qijiaFinish,QishouLeaveRecord.class);

		//遍历骑手请假记录表
		for (QishouLeaveRecord qishouLeaveRecord : finishList) {
			RiderAnalyzeVO analyzeVO = map.get(qishouLeaveRecord.getQishouUserid());
			if (analyzeVO == null) {
				analyzeVO = new RiderAnalyzeVO();
				analyzeVO.setNumSum(0);
				analyzeVO.setTimeSum(0.0);
				analyzeVO.setQishouName(qishouLeaveRecord.getQishouUsername());
				//计算请假时长,秒数换成小时
				//请假结束时间比筛选结束晚 但是请假开始时间在筛选时间段内
				if(qishouLeaveRecord.getEndTime() >= endTime && qishouLeaveRecord.getStartTime() > startTime && qishouLeaveRecord.getStartTime() < endTime) {
					analyzeVO.setTimeSum( DoubleUtil.doubleSplit((double)( endTime - qishouLeaveRecord.getStartTime() ) / 3600,2) );
					analyzeVO.setNumSum(analyzeVO.getNumSum() + 1);
				}
				//如果请假开始和结束时间在筛选时间范围内
				if (qishouLeaveRecord.getEndTime() <= endTime && qishouLeaveRecord.getEndTime() > startTime && qishouLeaveRecord.getStartTime() >= startTime && qishouLeaveRecord.getStartTime() < endTime) {
					analyzeVO.setNumSum(analyzeVO.getNumSum() + 1);
					analyzeVO.setTimeSum( DoubleUtil.doubleSplit( (double)( qishouLeaveRecord.getEndTime() - qishouLeaveRecord.getStartTime() ) / 3600,2) );
				}
				//如果请假结束时间在筛选时间之内，但是请假开始时间比筛选开始时间早
				if (qishouLeaveRecord.getEndTime() > startTime && qishouLeaveRecord.getEndTime() <= endTime && qishouLeaveRecord.getStartTime() <= startTime) {
					analyzeVO.setTimeSum( DoubleUtil.doubleSplit((double)( qishouLeaveRecord.getEndTime() - startTime  ) / 3600,2) );
					analyzeVO.setNumSum(analyzeVO.getNumSum() + 1);
				}
				//如果请假开始和结束时间包含了筛选的时段
				if (qishouLeaveRecord.getEndTime() > endTime && qishouLeaveRecord.getStartTime() < startTime) {
					analyzeVO.setTimeSum( DoubleUtil.doubleSplit((double)( endTime - startTime  ) / 3600,2) );
					analyzeVO.setNumSum(analyzeVO.getNumSum() + 1);
				}

				list.add(analyzeVO);
			}else {
				// analyzeVO已经存在了,只需要取出之前的值来在加上新的值即可
				//请假结束时间比筛选结束晚 但是请假开始时间在筛选时间段内
				if(qishouLeaveRecord.getEndTime() >= endTime && qishouLeaveRecord.getStartTime() > startTime && qishouLeaveRecord.getStartTime() < endTime) {
					analyzeVO.setTimeSum(analyzeVO.getTimeSum() + DoubleUtil.doubleSplit((double)( endTime - qishouLeaveRecord.getStartTime() ) / 3600,2));
					analyzeVO.setNumSum(analyzeVO.getNumSum() + 1);
				}
				//如果请假开始和结束时间在筛选时间范围内
				if (qishouLeaveRecord.getEndTime() <= endTime && qishouLeaveRecord.getEndTime() > startTime && qishouLeaveRecord.getStartTime() >= startTime && qishouLeaveRecord.getStartTime() < endTime) {
					analyzeVO.setNumSum(analyzeVO.getNumSum() + 1);
					analyzeVO.setTimeSum(analyzeVO.getTimeSum() + ( DoubleUtil.doubleSplit( (double)( qishouLeaveRecord.getEndTime() - qishouLeaveRecord.getStartTime() ) / 3600,2) ));
				}
				//如果请假结束时间在筛选时间之内，但是请假开始时间比筛选开始时间早
				if (qishouLeaveRecord.getEndTime() > startTime && qishouLeaveRecord.getEndTime() <= endTime && qishouLeaveRecord.getStartTime() <= startTime) {
					analyzeVO.setTimeSum(analyzeVO.getTimeSum() + DoubleUtil.doubleSplit((double)( qishouLeaveRecord.getEndTime() - startTime  ) / 3600,2) );
					analyzeVO.setNumSum(analyzeVO.getNumSum() + 1);
				}
				//如果请假开始和结束时间包含了筛选的时段
				if (qishouLeaveRecord.getEndTime() > endTime && qishouLeaveRecord.getStartTime() < startTime) {
					analyzeVO.setTimeSum(analyzeVO.getTimeSum() + DoubleUtil.doubleSplit((double)( endTime - startTime  ) / 3600,2) );
					analyzeVO.setNumSum(analyzeVO.getNumSum() + 1);
				}
			}
		}
		vo.setPage(page);
		vo.setList(list);
		//日志记录
		ActionLogUtil.insert(request, id , "查看代理商后台---骑手考勤分析");
		return vo;
	}

	/**
	 * 代理商后台--工作量数据分析
	 * @param startTime		筛选日期开始时间
	 * @param endTime		筛选日期结束时间
	 * @author zxy
	 * @throws ParseException 
	 */
	@ResponseBody
	@RequestMapping(value = "getWorkAnalyze.json",method = {RequestMethod.POST})
	public WorkAnalyzeListVO getWorkAnalyze(HttpServletRequest request,Model model,
			@RequestParam(value = "startTime",required = true,defaultValue = "0") Integer startTime,
			@RequestParam(value = "endTime",required = true,defaultValue = "0") Integer endTime) throws ParseException {
		int id = getUserId();
		WorkAnalyzeListVO vo = new WorkAnalyzeListVO();
		if (startTime > 0 && endTime <= 0) {
			vo.setBaseVO(BaseVO.FAILURE, "请选择时间范围");
			return vo;
		}
		if (startTime <= 0 && endTime > 0) {
			vo.setBaseVO(BaseVO.FAILURE, "请选择时间范围");
			return vo;
		}
		if (endTime < startTime) {
			vo.setBaseVO(BaseVO.FAILURE, "请选择正确的时间范围");
			return vo;
		}

		// 日期天数, 如果没有选择时间筛选条件 默认三十天
		int i = 30;
		if (startTime > 0 && endTime > 0) {
			String startday = DateUtil.intToString(startTime, "yyyyMMdd");
			String endday = DateUtil.intToString(endTime, "yyyyMMdd");
			i = AppUtil.daysBetween(startday, endday, "yyyyMMdd");
			// 最长30天
			if (i > 30) {
				vo.setBaseVO(BaseVO.FAILURE, "时间跨度最大不能超过30天");
				return vo;
			}
			// 最短1天
			if (i == 0) {
				vo.setBaseVO(BaseVO.FAILURE, "时间跨度最小1天");
				return vo;
			}
			// 获取天数
			i = i + 1;
			endTime = endTime + 86400;
		}else {
			endTime = DateUtil.getDateZeroTime(DateUtil.timeForUnix10());// 当日零点的时间戳 
			startTime = endTime - 30 * 86400;// 一月之前时间戳
		}
		// 日期数组
		int[] dateI = new int[i];
		Integer firstday = Integer.valueOf(DateUtil.intToString(startTime, "yyyyMMdd"));
		dateI[0] = firstday;
		for (int j = 1; j < dateI.length; j++) {
			// 在上个元素天数的基础上加一天
			dateI[j] = dateI[j-1] + 1;
			// 当前日期
			String dateString = dateI[j] + "";
			// 判断当前日是否是当前月最后一日
			int monthLastDay = AppUtil.getMonthLastDay(DateUtil.StringToInt(dateI[j-1] + "", "yyyyMMdd"), "yyyyMMdd");
			if (dateI[j] - monthLastDay == 1) {
				// 判断当前月是否是当前年最后一月
				if (Objects.equals(dateString.substring(4, 6), "12")) {
					// 跨年跨月
					Integer substring = Integer.valueOf(dateString.substring(0, 4));
					String yearFirst = AppUtil.getYearFirst(substring + 1,"yyyyMMdd");
					dateI[j] = Integer.valueOf(yearFirst);
				}else {
					// 只跨月不跨年
					String firstDayOfNextMonth = AppUtil.getFirstDayOfNextMonth(dateI[j-1] + "", "yyyyMMdd");
					dateI[j] = Integer.valueOf(firstDayOfNextMonth);
				}
			}
		}
		vo.setDateI(dateI);

		// 获取在这个时间段的订单
		String ordernow = "SELECT * FROM shop_order WHERE addtime > " + startTime + " AND  addtime < " + endTime + " AND dailishang_userid = " + id;
		List<Order> list = sqlService.findBySqlQuery(ordernow, Order.class);

		//数据容器 k: 日期 , v: 数据容器(k: 骑手id,v: WorkAnalyzeVO对象)
		Map<Integer, Map<Integer, WorkAnalyzeVO>> waicengriqimap = new TreeMap<Integer, Map<Integer,WorkAnalyzeVO>>();

		//遍历外层map get日期的key
		for (Order order : list) {
			//取出订单日期的时间 判断容器内是否有这个日期的数据
			Integer orderday = Integer.valueOf(DateUtil.intToString(order.getAddtime(), "yyyyMMdd"));
			Map<Integer, WorkAnalyzeVO> newmap = waicengriqimap.get(orderday);
			//如果没有 就新建一条数据
			if (newmap == null) {
				//newmap为空 那么数据必然为空 直接新建赋值  数据容器 k: 骑手id,v: WorkAnalyzeVO对象
				newmap = new HashMap<Integer, WorkAnalyzeVO>();
				WorkAnalyzeVO workAnalyzeVO = new WorkAnalyzeVO();
				//日期
				workAnalyzeVO.setRiqi(orderday);
				workAnalyzeVO.setQishouName(order.getQishouName());
				workAnalyzeVO.setDingdanShuliang(1);
				workAnalyzeVO.setWeight(order.getAllWeight());
				workAnalyzeVO.setMoney(order.getPayMoney());
				//总完成数量
				if (ObjectUtils.equals(order.getState(),Order.STATE_FINISH) || ObjectUtils.equals(order.getState(), Order.STATE_EVALUATED)) {
					workAnalyzeVO.setDoneNum(1);
				}else {
					workAnalyzeVO.setDoneNum(0);
				}
				//总未完成数量
				Integer noNum = workAnalyzeVO.getDingdanShuliang() - workAnalyzeVO.getDoneNum() < 0 ? 0 : workAnalyzeVO.getDingdanShuliang() - workAnalyzeVO.getDoneNum();
				workAnalyzeVO.setWeiwanchengNum(noNum);
				//订单总取消数量
				if (ObjectUtils.equals(order.getState(),Order.STATE_CANCEL)) {
					workAnalyzeVO.setQuxiaoNum(1);
				}else {
					workAnalyzeVO.setQuxiaoNum(0);
				}
				//订单异常数量
				if (ObjectUtils.equals(order.getState(), Order.STATE_DAIJIEDAN) || (order.getReserveEndTime() < DateUtil.timeForUnix10() && ObjectUtils.equals(order.getState(), Order.STATE_DAIQUHUO))) {
					workAnalyzeVO.setYqwanchengNum(1);
				}else {
					workAnalyzeVO.setYqwanchengNum(0);
				}
				//订单平均时间 待添加(1.取出订单中，已完成的订单 2.计算时间差赋值)
				if((ObjectUtils.equals(order.getState(),Order.STATE_FINISH ) || ObjectUtils.equals(order.getState(), Order.STATE_EVALUATED)) && order.getPayTime() != null && order.getPayTime() > order.getAddtime()) {
					workAnalyzeVO.setPingjunTime(order.getPayTime() - order.getAddtime());
				}else{
					workAnalyzeVO.setPingjunTime(0);			
				}
				//将数据填充进内层容器map
				newmap.put(order.getQishouId(), workAnalyzeVO);
				//将内层map加上日期填充进外层map
				waicengriqimap.put(orderday,newmap);
			}else {
				//如果该日期下newmap内已经有数据 那取出内层容器的key 判断内层容器是否有对应的数据
				WorkAnalyzeVO workAnalyzeVO = newmap.get(order.getQishouId());
				//如果没有当前k：骑手id 对应的数据 新建
				if(workAnalyzeVO == null) {
					workAnalyzeVO = new WorkAnalyzeVO();
					//日期
					workAnalyzeVO.setRiqi(orderday);
					workAnalyzeVO.setQishouName(order.getQishouName());
					workAnalyzeVO.setDingdanShuliang(1);
					workAnalyzeVO.setWeight(order.getAllWeight());
					workAnalyzeVO.setMoney(order.getPayMoney());
					//总完成数量
					if (ObjectUtils.equals(order.getState(),Order.STATE_FINISH) || ObjectUtils.equals(order.getState(), Order.STATE_EVALUATED)) {
						workAnalyzeVO.setDoneNum(1);
					}else {
						workAnalyzeVO.setDoneNum(0);
					}
					//总未完成数量
					Integer noNum = workAnalyzeVO.getDingdanShuliang() - workAnalyzeVO.getDoneNum() < 0 ? 0 : workAnalyzeVO.getDingdanShuliang() - workAnalyzeVO.getDoneNum();
					workAnalyzeVO.setWeiwanchengNum(noNum);
					//订单总取消数量
					if (ObjectUtils.equals(order.getState(),Order.STATE_CANCEL)) {
						workAnalyzeVO.setQuxiaoNum(1);
					}else {
						workAnalyzeVO.setQuxiaoNum(0);
					}
					//订单异常数量
					if (ObjectUtils.equals(order.getState(), Order.STATE_DAIJIEDAN) || (order.getReserveEndTime() < DateUtil.timeForUnix10() && ObjectUtils.equals(order.getState(), Order.STATE_DAIQUHUO))) {
						workAnalyzeVO.setYqwanchengNum(1);
					}else {
						workAnalyzeVO.setYqwanchengNum(0);
					}
					//订单平均时间 待添加(1.取出订单中，已完成的订单 2.计算时间差赋值)
					if((ObjectUtils.equals(order.getState(),Order.STATE_FINISH ) || ObjectUtils.equals(order.getState(), Order.STATE_EVALUATED)) && order.getPayTime() != null && order.getPayTime() > order.getAddtime()) {
						workAnalyzeVO.setPingjunTime(order.getPayTime() - order.getAddtime());
					}else{
						workAnalyzeVO.setPingjunTime(0);			
					}
					//将这条数据添加进内层容器中
					newmap.put(order.getQishouId(),workAnalyzeVO);
					//将内层map加上日期填充进外层map
					waicengriqimap.put(orderday,newmap);
				}else {
					workAnalyzeVO.setDingdanShuliang(workAnalyzeVO.getDingdanShuliang() + 1);
					workAnalyzeVO.setWeight(workAnalyzeVO.getWeight() + order.getAllWeight());
					workAnalyzeVO.setMoney(workAnalyzeVO.getMoney() + order.getPayMoney());
					//总完成数量
					if (ObjectUtils.equals(order.getState(),Order.STATE_FINISH) || ObjectUtils.equals(order.getState(), Order.STATE_EVALUATED)) {
						workAnalyzeVO.setDoneNum(workAnalyzeVO.getDoneNum() + 1);
					}
					//总未完成数量
					Integer noNum = workAnalyzeVO.getDingdanShuliang() - workAnalyzeVO.getDoneNum() < 0 ? 0 : workAnalyzeVO.getDingdanShuliang() - workAnalyzeVO.getDoneNum();
					workAnalyzeVO.setWeiwanchengNum(noNum);
					//订单总取消数量
					if (ObjectUtils.equals(order.getState(),Order.STATE_CANCEL)) {
						workAnalyzeVO.setQuxiaoNum(workAnalyzeVO.getQuxiaoNum() + 1);
					}
					//订单异常数量
					if (ObjectUtils.equals(order.getState(), Order.STATE_DAIJIEDAN) || (order.getReserveEndTime() < DateUtil.timeForUnix10() && ObjectUtils.equals(order.getState(), Order.STATE_DAIQUHUO))) {
						workAnalyzeVO.setYqwanchengNum(workAnalyzeVO.getYqwanchengNum() + 1);
					}
					//平均时间
					if((ObjectUtils.equals(order.getState(),Order.STATE_FINISH ) || ObjectUtils.equals(order.getState(), Order.STATE_EVALUATED)) && order.getPayTime() != null && order.getPayTime() > order.getAddtime()) {
						int chaNum = order.getPayTime() - order.getAddtime();
						workAnalyzeVO.setPingjunTime(workAnalyzeVO.getPingjunTime() + chaNum);
					}
					//将这条数据添加进内层容器中
					newmap.put(order.getQishouId(),workAnalyzeVO);
					//将内层map加上日期填充进外层map
					waicengriqimap.put(orderday,newmap);
				}	
			}
		}
		// 拿到日期-骑手的数据,列表展示用
		List<WorkAnalyzeVO> workAnalyzeVOs = new ArrayList<WorkAnalyzeVO>();
		// 按日期获取列表
		Map<Integer, WorkAnalyzePicVO> map = new TreeMap<Integer, WorkAnalyzePicVO>();
		waicengriqimap.forEach((riqi,neiMap) -> {
			WorkAnalyzePicVO analyzeVO = new WorkAnalyzePicVO(0, 0, 0, 0, 0, 0, 0.0, 0);
			analyzeVO.setRiqi(riqi);
			neiMap.forEach((qishouId,wAnalyzeVO) -> {
				workAnalyzeVOs.add(wAnalyzeVO);
				analyzeVO.setDingdanShuliang(analyzeVO.getDingdanShuliang() + wAnalyzeVO.getDingdanShuliang());				
				analyzeVO.setDoneNum(analyzeVO.getDoneNum() + wAnalyzeVO.getDoneNum());
				analyzeVO.setMoney(analyzeVO.getMoney() + wAnalyzeVO.getMoney());
				// 平均时间
				analyzeVO.setPingjunTime(analyzeVO.getPingjunTime() + wAnalyzeVO.getPingjunTime());
				analyzeVO.setQuxiaoNum(analyzeVO.getQuxiaoNum() + wAnalyzeVO.getQuxiaoNum());
				analyzeVO.setWeight(analyzeVO.getWeight() + wAnalyzeVO.getWeight());
				analyzeVO.setWeiwanchengNum(analyzeVO.getWeiwanchengNum() + wAnalyzeVO.getWeiwanchengNum());
				analyzeVO.setYqwanchengNum(analyzeVO.getYqwanchengNum() + wAnalyzeVO.getYqwanchengNum());
			});
			map.put(riqi, analyzeVO);
		});
		vo.setList(workAnalyzeVOs);

		for (int j = 0; j < dateI.length; j++) {
			WorkAnalyzePicVO workAnalyzePicVO = map.get(dateI[j]);
			if (workAnalyzePicVO == null) {
				workAnalyzePicVO = new WorkAnalyzePicVO(0, 0, 0, 0, 0, 0, 0.0, 0);
				workAnalyzePicVO.setRiqi(dateI[j]);
				map.put(dateI[j], workAnalyzePicVO);
			}
		}

		// 拿到日期-骑手的数据,折线图展示用
		List<WorkAnalyzePicVO> picVOs = new ArrayList<WorkAnalyzePicVO>();
		map.forEach((k,v) -> {
			picVOs.add(v);
		});

		// 骑手数组,长度为日期天数
		int[] dingdanShuliangArr = new int[i];
		double[] weightArr = new double[i];
		int[] doneNumArr = new int[i];
		int[] weiwanchengNumArr = new int[i];
		int[] quxiaoNumArr = new int[i];
		double[] money = new double[i];
		double[] pingjunTimeArr = new double[i];
		int[] yqwanchengNumArr = new int[i];
		if (picVOs.size() - i == 0) {
			for (int j = 0; j < picVOs.size(); j++) {
				Integer dingdanshuliang = picVOs.get(j).getDingdanShuliang();;
				dingdanShuliangArr[j] = dingdanshuliang;
				weightArr[j] = DoubleUtil.doubleSplit((double)picVOs.get(j).getWeight() / 1000,2);
				doneNumArr[j] = picVOs.get(j).getDoneNum();
				weiwanchengNumArr[j] = picVOs.get(j).getWeiwanchengNum();
				quxiaoNumArr[j] = picVOs.get(j).getQuxiaoNum();
				money[j] = DoubleUtil.doubleSplit((double)picVOs.get(j).getMoney() / 100,2);
				if(dingdanshuliang > 0) {
					pingjunTimeArr[j] = DoubleUtil.doubleSplit((double)picVOs.get(j).getPingjunTime() / dingdanshuliang / 60 ,2);
				}else {
					pingjunTimeArr[j] = picVOs.get(j).getPingjunTime();
				}

				yqwanchengNumArr[j] = picVOs.get(j).getYqwanchengNum();
			}
		}else {
			// 数据错误
			vo.setBaseVO(BaseVO.FAILURE, "数据异常,请联系技术人员");
			return vo;
		}

		// 折线图数据
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("订单总数量", dingdanShuliangArr);
		jsonObject.put("完成订单总重量", weightArr);
		jsonObject.put("总完成数量", doneNumArr);
		jsonObject.put("总未完成数量", weiwanchengNumArr);
		jsonObject.put("订单总取消数量", quxiaoNumArr);
		jsonObject.put("回收总金额", money);
		jsonObject.put("平均订单完成时间", pingjunTimeArr);
		jsonObject.put("异常订单数量", yqwanchengNumArr);

		vo.setJsonObject(jsonObject);
		//日志记录
		ActionLogUtil.insert(request, id, "查看代理商后台---骑手工作量数据分析");
		return vo;
	}

	/**
	 * 代理商后台--人效数据分析
	 * @param startTime		筛选日期开始时间
	 * @param endTime		筛选日期结束时间
	 * @author zxy
	 * @throws ParseException 
	 */
	@ResponseBody
	@RequestMapping(value = "getHumanEffectAnalyze.json",method = {RequestMethod.POST})
	public HumanEffectAnalyzeListVO getHumanEffectAnalyze(HttpServletRequest request,Model model,
			@RequestParam(value = "startTime",required = true,defaultValue = "0") Integer startTime,
			@RequestParam(value = "endTime",required = true,defaultValue = "0") Integer endTime) throws ParseException {
		int id = getUserId() ;//获取代理商id
		HumanEffectAnalyzeListVO vo = new HumanEffectAnalyzeListVO();
		if (startTime > 0 && endTime <= 0) {
			vo.setBaseVO(BaseVO.FAILURE, "请选择时间范围");
			return vo;
		}
		if (startTime <= 0 && endTime > 0) {
			vo.setBaseVO(BaseVO.FAILURE, "请选择时间范围");
			return vo;
		}
		if (endTime < startTime) {
			vo.setBaseVO(BaseVO.FAILURE, "请选择正确的时间范围");
			return vo;
		}

		// 日期天数, 如果没有选择时间筛选条件 默认三十天
		int i = 30;
		if (startTime > 0 && endTime > 0) {
			String startday = DateUtil.intToString(startTime, "yyyyMMdd");
			String endday = DateUtil.intToString(endTime, "yyyyMMdd");
			i = AppUtil.daysBetween(startday, endday, "yyyyMMdd");
			// 最长30天
			if (i > 30) {
				vo.setBaseVO(BaseVO.FAILURE, "时间跨度最大不能超过30天");
				return vo;
			}
			// 最短1天
			if (i == 0) {
				vo.setBaseVO(BaseVO.FAILURE, "时间跨度最小1天");
				return vo;
			}
			// 获取天数
			i = i + 1;
			endTime = endTime + 86400;
		}else {
			endTime = DateUtil.getDateZeroTime(DateUtil.timeForUnix10());// 当日零点的时间戳 
			startTime = endTime - 30 * 86400;// 一月之前时间戳
		}
		// 日期数组
		int[] dateI = new int[i];
		Integer firstday = Integer.valueOf(DateUtil.intToString(startTime, "yyyyMMdd"));
		dateI[0] = firstday;
		for (int j = 1; j < dateI.length; j++) {
			// 在上个元素天数的基础上加一天
			dateI[j] = dateI[j-1] + 1;
			// 当前日期
			String dateString = dateI[j] + "";
			// 判断当前日是否是当前月最后一日
			int monthLastDay = AppUtil.getMonthLastDay(DateUtil.StringToInt(dateI[j-1] + "", "yyyyMMdd"), "yyyyMMdd");
			if (dateI[j] - monthLastDay == 1) {
				// 判断当前月是否是当前年最后一月
				if (Objects.equals(dateString.substring(4, 6), "12")) {
					// 跨年跨月
					Integer substring = Integer.valueOf(dateString.substring(0, 4));
					String yearFirst = AppUtil.getYearFirst(substring + 1,"yyyyMMdd");
					dateI[j] = Integer.valueOf(yearFirst);
				}else {
					// 只跨月不跨年
					String firstDayOfNextMonth = AppUtil.getFirstDayOfNextMonth(dateI[j-1] + "", "yyyyMMdd");
					dateI[j] = Integer.valueOf(firstDayOfNextMonth);
				}
			}
		}
		vo.setDateI(dateI);
		// 获取在这个时间段的订单
		String ordernow = "SELECT * FROM shop_order WHERE addtime > " + startTime + " AND  addtime < " + endTime + " AND dailishang_userid = " + id;
		List<Order> list = sqlService.findBySqlQuery(ordernow, Order.class);
		//计算骑手数量
		int count = sqlService.count("laji_qishou"," WHERE state = " + Qishou.STATE_NORMAL + " AND dailishang_userid = " + id);
		vo.setCount(count);
		// 日期-数据对象
		Map<Integer, HumanEffectAnalyzeVO> map = new TreeMap<Integer, HumanEffectAnalyzeVO>();		
		for (Order order : list) {
			// 根据日期进行数据分发
			Integer orderday = Integer.valueOf(DateUtil.intToString(order.getAddtime(), "yyyyMMdd"));
			// 某天的数据,用于列表
			HumanEffectAnalyzeVO humanEffectAnalyzeVO = map.get(orderday);
			if (humanEffectAnalyzeVO == null) {
				humanEffectAnalyzeVO = new HumanEffectAnalyzeVO();
				//接单数量
				humanEffectAnalyzeVO.setRenjunjiedan(1);
				//支付金额（单位 分）
				humanEffectAnalyzeVO.setRenjunMoney(order.getPayMoney());
				//回收重量(单位 g)
				humanEffectAnalyzeVO.setRenjunWeight(order.getAllWeight());
				//订单完成时间（单位 分）
				if((ObjectUtils.equals(order.getState(), Order.STATE_FINISH) || ObjectUtils.equals(order.getState(), Order.STATE_EVALUATED)) && order.getPayTime() != null && order.getAddtime() != null){
					humanEffectAnalyzeVO.setPingjunTime( (order.getPayTime() - order.getAddtime() ) / 60);
				}else {
					humanEffectAnalyzeVO.setPingjunTime(0);
				}
				//异常订单数
				if(ObjectUtils.equals(order.getState(), Order.STATE_DAIJIEDAN) || (order.getReserveEndTime() < DateUtil.timeForUnix10() && ObjectUtils.equals(order.getState(), Order.STATE_DAIQUHUO))){
					humanEffectAnalyzeVO.setRenjunyichang(1);
				}else {
					humanEffectAnalyzeVO.setRenjunyichang(0);
				}
				// 日期
				humanEffectAnalyzeVO.setRiqi(Integer.valueOf(DateUtil.intToString(order.getAddtime(), "yyyyMMdd")));
				// 添加到map
				map.put(orderday, humanEffectAnalyzeVO);

			}else {
				//接单
				humanEffectAnalyzeVO.setRenjunjiedan(humanEffectAnalyzeVO.getRenjunjiedan() + 1);
				//金额
				humanEffectAnalyzeVO.setRenjunMoney(humanEffectAnalyzeVO.getRenjunMoney() + order.getPayMoney());
				//重量
				humanEffectAnalyzeVO.setRenjunWeight(humanEffectAnalyzeVO.getRenjunWeight() + order.getAllWeight());
				//时间（单位 分）
				if((ObjectUtils.equals(order.getState(), Order.STATE_FINISH) || ObjectUtils.equals(order.getState(), Order.STATE_EVALUATED)) && order.getPayTime() != null && order.getAddtime() != null){
					humanEffectAnalyzeVO.setPingjunTime(humanEffectAnalyzeVO.getPingjunTime() + (order.getPayTime() - order.getAddtime()) / 60);
				}else {
					humanEffectAnalyzeVO.setPingjunTime(humanEffectAnalyzeVO.getPingjunTime());
				}

				//异常订单数
				if(ObjectUtils.equals(order.getState(), Order.STATE_DAIJIEDAN) || (order.getReserveEndTime() < DateUtil.timeForUnix10() && ObjectUtils.equals(order.getState(), Order.STATE_DAIQUHUO))){
					humanEffectAnalyzeVO.setRenjunyichang(humanEffectAnalyzeVO.getRenjunyichang() + 1);
				}else {
					humanEffectAnalyzeVO.setRenjunyichang(humanEffectAnalyzeVO.getRenjunyichang());
				}
				// 添加到map
				map.put(orderday, humanEffectAnalyzeVO);

			}
		}
		// 根据日期补全数据
		for (int j = 0; j < dateI.length; j++) {
			HumanEffectAnalyzeVO humanEffectAnalyzeVO = map.get(dateI[j]);
			if (humanEffectAnalyzeVO == null) {
				humanEffectAnalyzeVO = new HumanEffectAnalyzeVO(dateI[j], 0, 0, 0, 0, 0);
				map.put(dateI[j], humanEffectAnalyzeVO);
			}
		}

		// 数据列表--返回列表使用

		List<HumanEffectAnalyzeVO> humanEffectAnalyzeVOs = new ArrayList<HumanEffectAnalyzeVO>();
		for (HumanEffectAnalyzeVO hAnalyzeVO : map.values()) {
			humanEffectAnalyzeVOs.add(hAnalyzeVO);
		}
		vo.setList(humanEffectAnalyzeVOs);
		// 订单量数组,长度为日期天数
		double[] renjunjiedanArr = new double[i];
		double[] renjunMoneyArr = new double[i];
		double[] renjunWeightArr = new double[i];
		double[] pingjunTimeArr = new double[i];
		double[] renjunyichangArr = new double[i];
		if (humanEffectAnalyzeVOs.size() - i == 0) {
			for (int j = 0; j < humanEffectAnalyzeVOs.size(); j++) {
				if (humanEffectAnalyzeVOs.get(j).getRenjunjiedan() > 0 && count > 0) {
					renjunjiedanArr[j] = DoubleUtil.doubleSplit((double)humanEffectAnalyzeVOs.get(j).getRenjunjiedan() / count ,2 );
				}else {
					renjunjiedanArr[j] = DoubleUtil.doubleSplit( (double)humanEffectAnalyzeVOs.get(j).getRenjunjiedan(),2);
				}
				if (humanEffectAnalyzeVOs.get(j).getRenjunMoney() > 0 && count > 0) {
					renjunMoneyArr[j] = DoubleUtil.doubleSplit((double)humanEffectAnalyzeVOs.get(j).getRenjunMoney() / count / 100.0,2);
				}else {
					renjunMoneyArr[j] = DoubleUtil.doubleSplit((double)humanEffectAnalyzeVOs.get(j).getRenjunMoney(),2);
				}
				if (humanEffectAnalyzeVOs.get(j).getRenjunWeight() > 0 && count > 0) {
					renjunWeightArr[j] = DoubleUtil.doubleSplit((double)humanEffectAnalyzeVOs.get(j).getRenjunWeight() / count / 1000.0,2);
				}else {
					renjunWeightArr[j] = DoubleUtil.doubleSplit((double)humanEffectAnalyzeVOs.get(j).getRenjunWeight(),2);
				}
				//平均时间
				if (humanEffectAnalyzeVOs.get(j).getPingjunTime() > 0 && count > 0) {
					pingjunTimeArr[j] = DoubleUtil.doubleSplit((double)humanEffectAnalyzeVOs.get(j).getPingjunTime() / count  ,2);
				}else {
					pingjunTimeArr[j] = DoubleUtil.doubleSplit((double)humanEffectAnalyzeVOs.get(j).getPingjunTime(),2);
				}
				if (humanEffectAnalyzeVOs.get(j).getRenjunyichang() > 0 && count > 0) {
					renjunyichangArr[j] = DoubleUtil.doubleSplit( (double)humanEffectAnalyzeVOs.get(j).getRenjunyichang() / count ,2 ) ;
				}else {
					renjunyichangArr[j] = DoubleUtil.doubleSplit( (double)humanEffectAnalyzeVOs.get(j).getRenjunyichang() ,2);
				}
			}
		}else {
			// 数据错误
			vo.setBaseVO(BaseVO.FAILURE, "数据异常,请联系技术人员");
			return vo;
		}

		// 折线图数据
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("人均接单数量", renjunjiedanArr);
		jsonObject.put("人均回收金额", renjunMoneyArr);
		jsonObject.put("人均回收重量", renjunWeightArr);
		jsonObject.put("平均订单完成时间", pingjunTimeArr);
		jsonObject.put("人均订单异常量", renjunyichangArr);

		vo.setJsonObject(jsonObject);
		//日志记录
		ActionLogUtil.insert(request, id, "查看代理商后台---人效数据分析");
		return vo;
	}


}
